'use strict';

import { customer_orderwineModel } from "../model/customer_orderwine";
import { ServiceBase, Exceptions } from '../kalend';
import { merchant_wineService } from "./merchant_wine";

class Customer_orderwineService extends ServiceBase {
    constructor() {
        super(customer_orderwineModel)
    }
    async queryWineCost(mid: any) {
        const winelist = await merchant_wineService.queralls({ merchantid: mid, status: 1 }, 'winename', { order: 1 });
        let wins = winelist.map((a) => {
            return a._id;
        });
        let data = await customer_orderwineModel.aggregate([
            {
                $match: { wineid: { $in: wins } }
            },
            {
                $group: {
                    "_id": null,//"$wineid",
                    "total": {
                        "$sum": "$count"
                    }
                }
            },
            {
                $project: {
                    "_id": 0,
                    "total": 1
                }
            }
        ]);
        if (data && data.length > 0) {
            return data[0].total;
        } else {
            return 0;
        }
    }
    async queryWineCostByMid(mid: any){
        const winelist = await merchant_wineService.queralls({ merchantid: mid, status: 1 }, 'winename', { order: 1 });
        let wins = winelist.map((a) => {
            return a._id;
        });
        let data = await customer_orderwineModel.aggregate([
            {
                $match: { wineid: { $in: wins } }
            },
            {
                $group: {
                    "_id": "$wineid",
                    "total": {
                        "$sum": "$count"
                    }
                }
            },
            {
                $project: {
                    "_id": 1,
                    "total": 1
                }
            }
        ]);
        return data;
    }
}

export const customer_orderwineService = new Customer_orderwineService();